import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    will() {

    }
    @o userList = [];
    @o currentPage = 1;
    @o userTotal = 0;
    @o label = '';
    @o keyword = '';
    @o recordKeyword = '';
    @o startTime = '';
    @o recordStartTime = '';
    @o endTime = '';
    @o recordEndTime = '';
    @o firstRequest = false;

    @a
    paginationChange(current) {
        this.currentPage = current;
        this.getUsers(true);
    }

    @a
    getUsers(loading){
        loading && this.stores.Frame.showLoading();
        request({
            url:$$.api('getUsers'),
            method:'post',
            data:{
                pageNumber: this.currentPage,
                pageSize: $$.SEM.USER_LIST_SIZE,
                starteTime:this.recordStartTime || null,
                endTime:this.recordEndTime || null,
                searchCriteria:this.recordKeyword || null,
                label:this.label || null,
            }
        }).then(res => {
            this.stores.Frame.hideLoading();
            this.firstRequest = true;
            this.userList = res.data.userList;
            this.userTotal = res.data.resultTotalNumber;
        }).catch(err =>{
            this.stores.Frame.hideLoading();
            this.stores.Message.fire('message', {
                type: $$.SEM.ERROR_TEXT,
                text: err.message
            });
        })
    }
    @a
    startTimeChange(e) {
        this.startTime = e._d && e._d.getTime();
    }

    @a
    endTimeChange(e) {
        this.endTime = e._d && e._d.getTime();
    }

    @a
    clearStartTime() {
        this.startTime = '';
    }

    @a
    clearEndTime() {
        this.endTime = '';
    }

    identityChange(v) {
        this.label = v || '';
        this.search()
    }

    @a
    nameChange(v) {
        this.keyword = v;
    }
    @a
    closeHandle() {
        this.keyword = '';
        this.search();
    }

    @a
    nameInput(e) {
        if (e.which === 13) {
            this.search();
        }
    }

    @a
    search() {
        if (!this.startTime && this.endTime) {
            this.stores.Message.fire('message', {type: $$.SEM.WARNING_TEXT, text: '开始时间不能为空'});
            return;
        }
        if (this.startTime && !this.endTime) {
            this.stores.Message.fire('message', {type: $$.SEM.WARNING_TEXT, text: '结束时间不能为空'});
            return;
        }
        if (this.startTime > this.endTime) {
            this.stores.Message.fire('message', {
                type: $$.SEM.WARNING_TEXT,
                text: '开始时间不能大于结束时间'
            });
            return;
        }
        if (this.startTime && this.endTime && this.startTime === this.endTime) {
            this.stores.Message.fire('message', {
                type: $$.SEM.WARNING_TEXT,
                text: '开始时间不能和结束时间相同'
            });
            return;
        }
        this.recordStartTime= this.startTime;
        this.recordEndTime  = this.endTime;
        this.recordKeyword = this.keyword;
        this.currentPage= 1;
        this.getUsers(true)
    }

    searchTime(){
        if (!this.startTime && !this.endTime) {
            this.search()
        }
        this.search()
    }

    getSortDelete(id){

    }

    @a
    editClick(data){
        this.stores.UsersEdit.fire('usersedit:show',{
            detail:data,
            display:'edit',
        })
    }

    sortClick(data){
        this.stores.SortDialog.fire('sortDialog:show', {
            sort: data.sort,
            confirmFunc: (sort) => {
                this.stores.Frame.showLoading();
                request({
                    url: $$.api('sortUser'),
                    method: 'put',
                    data: {
                        sort: sort,
                        id: data.id
                    }
                }).then(res => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(1, res.message);
                    this.getUsers();
                }).catch(err => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(2, err.message);
                })
            }
        })
    }

    @a
    deleteSort(data){
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips:'清除排序后，用户将不显示在首页',
            func: () => {
                this.stores.Frame.showLoading();
                request({
                    url:`${$$.api('deleteSort')}?id=${data.id}`,
                    method:'put',
                }).then(res => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(1, res.message);
                    this.getUsers();
                }).catch(err => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(2, err.message);
                })
            }
        });
    }

    @a
    addNewUsers(){
        this.stores.UsersEdit.fire('usersedit:show',{
            display:'add',
        })
    }

    getUserExcel(){
        request({
            url:$$.api('downloadUserExcel'),
            method:'post',
            data:{
                starteTime:this.recordStartTime || null,
                endTime:this.recordEndTime || null,
                searchCriteria:this.recordKeyword || null,
                label:this.label || null,
            }
        }).then(res => {
            this.stores.Message.showMessage(1, '导出成功');
            this.downUrl = res.data.url;
            let a = document.createElement('a');
            a.href = 'http://' + this.downUrl;
            a.download = '用户数据';
            a.click();
        }).catch(err =>{
            this.stores.Message.showMessage(2, err.message);
        })
    }
    @a
    downloadExcel(){
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips:'确定导出当前所选用户记录吗？',
            func: () => {
                this.getUserExcel()
            }
        });
    }


    mount() {
        this.getUsers()
    }

}

export default Store;